﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using ObjectTransposeApi;
using System.Collections;

namespace ObjectTransposeApiSystemTest.TestFixture
{
    [TestFixture]
    public class ToValueIndexMapperTestFixture
    {
        public class ChildStructure
        {
            public int Index { get; set; }
            public string Value { get; set; }
        }

        public class ParentStructure
        {
            public List<ChildStructure> ChildStructures { get; set; }
        }

        [Test]
        public void SimpleTest()
        {
            List<object> values = new List<object>();

            // add values
            values.Add(new { Index = 1, Value = "apple" });
            values.Add(new { Index = 1, Value = "banana" });
            values.Add(new { Index = 2, Value = "grape" });
            values.Add(new { Index = "2", Value = "mango" });
            values.Add(new { Index = 7, Value = "mandarine" });
            values.Add(new { Index = 7, Value = "pear" });
            values.Add(new { Index = "9", Value = "peach" });

            var sources = new
            {
                ChildStructures = values
            };

            IndexMapper indexMapper = new IndexMapper("ChildStructures", "Index");

            ParentStructure parentStructure = ObjectToValue.Copy<ParentStructure>(
                sources,
                new IndexMapper[] {indexMapper});

            // make sure the field was set
            Assert.IsNotNull(parentStructure.ChildStructures);
            Assert.AreEqual(parentStructure.ChildStructures.Count, 3);
            Assert.AreEqual(parentStructure.ChildStructures[0].Index, 1);
            Assert.AreEqual(parentStructure.ChildStructures[0].Value, "banana");
            Assert.AreEqual(parentStructure.ChildStructures[1].Index, 2);
            Assert.AreEqual(parentStructure.ChildStructures[1].Value, "grape");
            Assert.AreEqual(parentStructure.ChildStructures[2].Index, 7);
            Assert.AreEqual(parentStructure.ChildStructures[2].Value, "pear");
        }
    }
}
